<?php

include_once 'Answer_Patterns.php';
include_once 'Mount_DOM_tree.php';

class Refactoring_Form_Unlabeled extends Answer_Patterns {

   public function start($domDocument) {
      $result = "";
      $mount_tree = new Mount_DOM_tree();

      $search = $domDocument->getElementsByTagName('form');

      foreach ($search as $f) {
         $inputs = $f->getElementsByTagName('input');
//         echo "<pre>".print_r($inputs->item(0)->parentNode)."</pre>";
         $node_father = $inputs->item(0)->parentNode;

         $flag = false;

         $answer_HTML = '&LT;' . $f->nodeName . '&GT<br>...';

         for ($i = 1; $i < $node_father->childNodes->length; $i++) {
            if (!empty(trim($node_father->childNodes->item($i - 1)->wholeText)) &&
                    $node_father->childNodes->item($i - 1)->nodeName == "#text" &&
                    $node_father->childNodes->item($i)->nodeName == "input") {

//               print_r($node_father->childNodes->item($i-1)); echo "<br>";
//               print_r($node_father->childNodes->item($i));echo "<br>";echo "<br>";echo "<br>";
//               
//               echo "possível refatoralçao 11 ". $node_father->childNodes->item($i-1)->nodeName . "/" .$node_father->childNodes->item($i)->nodeName;
               $answer = '&LT;label&GT';
               $answer_HTML .= '<br>&nbsp;&nbsp;&LT;label for="' . $inputs->item(0)->getAttribute('name') . '"&GT;' . trim($node_father->childNodes->item($i - 1)->wholeText) . '&LT;/label&GT;<br>
                              &nbsp;&nbsp;&LT;div&GT;&LTinput type="' . $inputs->item(0)->getAttribute('type') . '" id="' . $inputs->item(0)->getAttribute('id') . '" name="' . $inputs->item(0)->getAttribute('name') . '"/&GT&LT/div&GT
                              ';
               $flag = true;
//               break;
            }
         }
         if ($flag) {
            $answer_HTML .= '<br>...<br>&LT;/' . $f->nodeName . '&GT';
            $result .= parent::elementAnswerPatterns("&LT;form&GT;", $node_father->getLineNo(), $answer, "Adicione os rótulos ", " e falta alguns rótulos nas entradas de dados");
            $result .= parent::tableAnswer($mount_tree->structHTML($node_father, 3, 0), $answer_HTML);
         }
      }

      if (!empty($result)) {
         $result = parent::titleAnswerPatterns("Adicionar rótulos à entrada de dados") . $result;
      }

      return $result;
   }

}
